package tech.six6.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import tech.six6.entity.MybatisSpring;
import tech.six6.service.MybatisSpringService;

import java.util.List;

@Controller("mybatisSpringController")
public class MybatisSpringController {

    @Autowired
    private MybatisSpringService mybatisSpringService;



    @Test
    public void insert(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        MybatisSpringController mybatisSpringController = (MybatisSpringController)applicationContext.getBean("mybatisSpringController");
        MybatisSpring mybatisSpring = new MybatisSpring();
        mybatisSpring.setName("王五111");
        mybatisSpring.setPassword("543215");
        mybatisSpring.setSex("保密");
        if (mybatisSpringController.mybatisSpringService.add(mybatisSpring)>0){
            System.out.println("增加成功");
        }else{
            System.out.println("增加失败");
        }
    }

    @Test
    public void delete(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        MybatisSpringController mybatisSpringController = (MybatisSpringController)applicationContext.getBean("mybatisSpringController");

        int id = 13;
        if (mybatisSpringController.mybatisSpringService.delete(id)>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    @Test
    public void update(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        MybatisSpringController mybatisSpringController = (MybatisSpringController)applicationContext.getBean("mybatisSpringController");

        MybatisSpring mybatisSpring = new MybatisSpring();
//        mybatisSpring.setName("王五新");
//        mybatisSpring.setPassword("111543215");
//        mybatisSpring.setSex("保密");
        mybatisSpring.setId(16);
        if (mybatisSpringController.mybatisSpringService.update(mybatisSpring)>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

    @Test
    public void selectById(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        MybatisSpringController mybatisSpringController = (MybatisSpringController)applicationContext.getBean("mybatisSpringController");

        int id = 14;
        MybatisSpring mybatisSpring = mybatisSpringController.mybatisSpringService.selectById(id);
        System.out.println(mybatisSpring);
    }

    @Test
    public void selectAll(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        MybatisSpringController mybatisSpringController = (MybatisSpringController)applicationContext.getBean("mybatisSpringController");

        List<MybatisSpring> mybatisSprings = mybatisSpringController.mybatisSpringService.selectAll();
        mybatisSprings.forEach(x-> System.out.println(x));
    }

}
